import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tiktokadult/hj_page/mine_business/message/message_center_sub_controller.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';

import '../../../main.dart';

enum MessageType {
  session(0), //私信
  comment(1), //评论
  thumb(2); //点赞

  final int type;
  const MessageType(this.type);
}

class MessageCenterSubPage extends StatefulWidget {
  final MessageType type;
  const MessageCenterSubPage(this.type, {super.key});

  @override
  State<MessageCenterSubPage> createState() => _MessageCenterSubPageState();
}

class _MessageCenterSubPageState extends State<MessageCenterSubPage> with RouteAware {
  late MessageCenterSubLogic logic;

  @override
  void initState() {
    super.initState();
    logic = instanceController(widget.type);
  }

  void didChangeDependencies() {
    super.didChangeDependencies();
    routeObserver.subscribe(this, ModalRoute.of(context)!);
  }

  @override
  void didPopNext() {
    super.didPopNext();
  }

  @override
  dispose() {
    super.dispose();
    routeObserver.unsubscribe(this);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MessageCenterSubLogic>(
      init: logic,
      global: false,
      builder: (controller) => pullYsRefresh(
          onLoading: (_) => controller.fetchPageData(isRefresh: false),
          onRefresh: (_) => controller.fetchPageData(),
          onInit: (_) => controller.refreshController = _,
          child: () {
            if (controller.isLoading) return LoadingCenterWidget();
            if (controller.dataSource.isEmpty) return CErrorWidget();
            return ListView.builder(
              padding: EdgeInsets.only(left: 16, right: 16, top: 12),
              itemCount: controller.dataSource.length,
              itemBuilder: (BuildContext context, int index) {
                return controller.instanceChildItem(index);
              },
            );
          }()),
    );
  }
}
